<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	require_once dirname (BASEPATH) . '/application/controllers/system/_init.php';
	require_once dirname (BASEPATH) . '/application/libraries/facebook_api.php';
	
	abstract class abs_member extends _init
	{	
		function abs_member(){
			parent::__construct();
			$this->template->set_template('left_column');
		}
		
		private function member_info(){
			$member_id = $this->session->userdata('MEM_ID');
				
			$this->load->model('local/data/mod_member');
			$this->load->library('facebook_api');
							
			$data['member'] = $this->mod_member->get_member($member_id);
			$data['fb_avatar'] = $this->facebook_api->get_user_avatar($data['member']['MEM_FB_UID']);
			
			return $data;
		}
		
		// Page	
		function index(){
			if ($this->check_member_session()){
				$this->personnel();
			}else{
				$this->template->set_template('default');
				$this->set_header_main();
				$this->set_navigation(array('<i class="icon-home"></i>หน้าแรก' => base_url(),
											'สมาชิก' => base_url() . 'member'));

				$facebook = new facebook_api(false);
				$data['app_id'] = $facebook->facebook_app_id;
				
				$this->template->write('content_1', $this->load->view('member/frm_member_login', $data, true));
				$this->template->render();
			}				
		}		
		function personnel(){
				$opr = $this->uri->segment(3);
				
				switch ($opr){
					case 'ajax_save':{
						$result = false;
						$message = null;
						
						$this->load->library('define/operation_message');
						if ($this->check_member_session()){					
							$datestr = $this->input->post('mby') . '-' . $this->input->post('mbm') . '-' . $this->input->post('mbd');
							
							$this->load->model('local/data/mod_member');
							$this->mod_member->MEM_TITLE = $this->input->post('mt');	
							$this->mod_member->MEM_BIRTH_DATE = date('Y-m-d', strtotime($datestr)); 				
							$this->mod_member->MEM_ADDRESS = $this->input->post('mads');
							$this->mod_member->MEM_TEL = $this->input->post('mtel');
							$this->mod_member->MEM_EMAIL = $this->input->post('mm');
							if ($this->mod_member->edit($this->session->userdata('MEM_ID'))){
								$result = true;
								$message = $this->operation_message->msg_save_success;
							} else
								$message = $this->operation_message->msg_save_fail;
						} else 
							$message = $this->operation_message->msg_relogin;					
	
						print_r(json_encode(array('result' => $result,
													'message' => $message)));					
					}
					break;
					default:{
						if ($this->check_member_session()){
							$this->set_header_main();
							$this->set_navigation(array('<i class="icon-home"></i>หน้าแรก' => base_url(),
														'สมาชิก' => base_url() . 'member',
														'ข้อมูลส่วนตัว' => base_url() . 'member/personnel'));
							
							$this->load->library('custom_datetime');
							$data_content['months'] = $this->custom_datetime->month_thai();
							
							$this->load->model('local/data/mod_member');
							$data_content['item'] = $this->mod_member->get_member($this->session->userdata('MEM_ID'), array('MEM_TITLE','MEM_BIRTH_DATE','MEM_ADDRESS','MEM_TEL','MEM_EMAIL'));
							
							$data_menu['user'] = $this->member_info();
							$data_menu['active_menu'] = 'personnel';										
								
							$this->template->write('content_1', $this->load->view('member/frm_member_personnel', $data_content, true));
							$this->template->write('left_sidebar', $this->load->view('widget/member/frm_user_info', $data_menu, true));
							$this->template->render();	
						} else
							redirect(base_url() . 'member');	
					}
					break;
				}
		}	
		function account(){
			
				$opr = $this->uri->segment(3);			
				switch ($opr){
					case 'ajax_change_password':{
						$result = false;
						$message = null;
						
						$this->load->library('define/operation_message');
						if ($this->check_member_session()){
							$this->load->model('local/data/mod_member');
							switch ($this->mod_member->change_password($this->session->userdata('MEM_ID'), $this->input->post('anp'), $this->input->post('aop'))){
								case true: case 1:
										$message = $this->operation_message->msg_save_success;
										$result = true;
									break;
								case false: case 0:
										$message = $this->operation_message->msg_save_fail;
									break;
								case -1:
										$message = $this->operation_message->msg_old_password_invalid;
									break;
							}					
						} else
							$message = $this->operation_message->msg_relogin;
							
						print_r(json_encode(array('result' => $result,
													'message' => $message)));
					}
					break;
					default:{
						if ($this->check_member_session()){
							$this->set_header_main();
							$this->set_navigation(array('<i class="icon-home"></i>หน้าแรก' => base_url(),
									'สมาชิก' => base_url() . 'member',
									'บัญชีผู้ใช้' => base_url() . 'member/account'));
							
							$this->load->model('local/data/mod_member');
							$data_content['item'] = $this->mod_member->get_member($this->session->userdata('MEM_ID'), array('MEM_EMAIL','MEM_PASS'));
							
							$data_menu['user'] = $this->member_info();
							$data_menu['active_menu'] = 'account';						
								
							$this->template->write('content_1', $this->load->view('member/frm_member_account', $data_content, true));
							$this->template->write('left_sidebar', $this->load->view('widget/member/frm_user_info', $data_menu, true));					
							$this->template->render();
						} else
							redirect(base_url() . 'member');
					}
					break;
				}
		}
		function cafe(){
			
			$opr = $this->uri->segment(3);				
			switch ($opr) {
				case 'ajax_save':{
					$result = 0;
					$message = null;
					$redirect_url = base_url() . 'member/logout';

					$this->load->library('define/operation_message');		
					switch (trim($this->check_member_session())){								
						case 1:{
							$this->load->library('define/general_define');
							$this->load->model('local/data/cafe/mod_cafe');
								
							// check own cafe count
							if ($this->mod_cafe->get_count($this->session->userdata('MEM_ID')) < $this->general_define->maximum_own_cafe_limit){							
								// check string length
								if (strlen($this->input->post('accn')) >= 5 && strlen($this->input->post('accn') <= 12)){							
									$this->load->library('string_function');					
										
									// validate string
									if ($this->string_function->validate_username($this->input->post('accn'))){
										$this->load->library('file_function');
										//	check available name and reserve word 							
										if ($this->mod_cafe->check_available_name($this->input->post('accn') && $this->file_function->check_inreserve_filename($this->input->post('accn')))){										
											// create controller file
											$this->file_function->cafe_name = strtolower($this->input->post('accn'));
											$this->file_function->filename = strtolower($this->input->post('accn') . '.php');
											if ($this->file_function->create_file()){												
													
												// add data to db
												$this->mod_cafe->CAF_CODE = $this->mod_cafe->generate_next_cafe_code();
												$this->mod_cafe->CAF_TITLE = $this->input->post('acct');
												$this->mod_cafe->CAF_NAME = strtolower($this->input->post('accn'));
												$this->mod_cafe->CAF_STATUS = true;
												$this->mod_cafe->MEM_ID = $this->session->userdata('MEM_ID');
												if ($this->mod_cafe->add()){								
													if (is_array($this->input->post('actp')) && (count($this->input->post('actp') > 0))){			
														$this->load->model('local/data/cafe/mod_cafe_type_list');												
														$this->mod_cafe_type_list->CAF_T_L_LIST = implode(',', $this->input->post('actp'));
														$this->mod_cafe_type_list->CAF_ID = $this->mod_cafe->db()->insert_id();
														$this->mod_cafe_type_list->add();
													}												
														
													$message = $this->operation_message->msg_save_success;
													$result = 1; 
												} else
													$message = $this->operation_message->msg_save_fail;											
											} else
												$message = $this->operation_message->msg_save_fail;
										} else {
											$message = $this->operation_message->msg_is_not_available_cafe_name($this->input->post('accn'));
											$result = -2;
										}										
									} else {
										$message = $this->operation_message->msg_invalid_cafe_name;
										$result = -2;
									}		
								} else {
									$message = $this->operation_message->msg_invalid_char_count;
									$result = -2;
								}
							} else {
								$message = $this->operation_message->msg_own_cafe_over_limit($this->general_define->maximum_own_cafe_limit);
								$result = -3;
							}								
						}
						break;
						case 0: default:{
							$result = -1;
							$message = $this->operation_message->msg_relogin;
							$redirect_url = base_url() . 'member/logout';									
						}
						break;
						case -2:{
							$result = -2;
							$message = $this->operation_message->msg_authen_fail;
							$redirect_url = base_url() . 'member/logout';									
						}
						break;								
					}
							
					print_r(json_encode(array('result' => $result,
												'message' => $message,
												'redirect_url' => $redirect_url)));
				}				
				break;
				case 'ajax_remove_item':{
					$result = false;
					$message = null;
					
					$this->load->library('define/operation_message');					
					if (!$this->check_member_session()){
						print_r(json_encode(array('result' => -1,
													'message' => $this->operation_message->msg_relogin,
													'redirect_url' => base_url() . 'member')));
					} else {
						$result = false;
						$message = null;
						
						$this->load->model('local/data/cafe/mod_cafe');
						if ($this->mod_cafe->remove_by_code($this->input->post('c'))){
							$result = true;
							$message = $this->operation_message->msg_remove_success;
						} else
							$message = $this->operation_message->msg_remove_fail;
							
						print_r(json_encode(array('result' => $result,
													'message' => $message)));
					}
				}
				break;				
				case 'ajax_get_cafe_type':{
					$this->load->model('local/data/cafe/mod_cafe_type');
					$result = $this->mod_cafe_type->get_cafe_type_list();

					$return = array();
					for ($i=0; $i<count($result); $i++){
						array_push($return, array('id' => $result[$i]['CAF_T_ID'],
													'name' => $result[$i]['CAF_T_TITLE']));
					}
					print_r(json_encode($return));
				}
				break;
				case 'ajax_check_name':{
					$result = 0;
					$message = null;
					$redirect_url = null;

					$this->load->library('define/operation_message');		
					switch (trim($this->check_member_session())){
								
						case 1:{
							if (strlen($this->input->post('accn')) >= 5 && strlen($this->input->post('accn') <= 12)){								
								$this->load->library('string_function');								
								if ($this->string_function->validate_username($this->input->post('accn'))){													
									
									$this->load->model('local/data/cafe/mod_cafe');
									$this->load->library('file_function');
									
									if ($this->mod_cafe->check_available_name($this->input->post('accn')) && $this->file_function->check_inreserve_filename($this->input->post('accn'))){
										$result = 1;
										$message = $this->operation_message->msg_is_available_cafe_name($this->input->post('accn'));
									} else
										$message = $this->operation_message->msg_is_not_available_cafe_name($this->input->post('accn'));
								} else							
									$message = $this->operation_message->msg_invalid_cafe_name;								
							} else 
								$message = $this->operation_message->msg_invalid_char_count;							
						}
						break;
						case 0: default:{
							$result = -1;
							$message = $this->operation_message->msg_relogin;
							$redirect_url = base_url() . 'member/logout';									
						}
						break;
						case -2:{
							$result = -2;
							$message = $this->operation_message->msg_authen_fail;
							$redirect_url = base_url() . 'member/logout';									
						}
						break;								
					}
					
					print_r(json_encode(array('result' => $result,
												'message' => $message,
												'redirect_url' => $redirect_url)));		
				}
				break;
				default:{
					if ($this->check_member_session()){
						$this->set_header_main();
						$this->set_navigation(array('<i class="icon-home"></i>หน้าแรก' => base_url(),
													'สมาชิก' => base_url() . 'member',
													'ร้านกาแฟของฉัน' => base_url() . 'member/cafe'));
								
						$data_menu['user'] = $this->member_info();
						$data_menu['active_menu'] = 'cafe';
						
						$this->load->model('local/data/cafe/mod_cafe');
						$data_content['data'] = $this->mod_cafe->get_cafe_of_member($this->session->userdata('MEM_ID'));						
														
						$this->template->write('left_sidebar', $this->load->view('widget/member/frm_user_info', $data_menu, true));
						$this->template->write('content_1', $this->load->view('member/frm_member_cafe', $data_content, true));							
						$this->template->render();
					} else
						redirect(base_url() . 'member');
				}
				break;
			}		
		}
		function payment(){
			$opr = $this->uri->segment(3);				
			switch ($opr) {
				case 'ajax_view':{						
						$this->load->model('local/data/mod_package_order');
						$this->mod_package_order->get_single_order($this->input->post('c'));
					}	
					break;
				default:
					{	
						if ($this->check_member_session()){
							$this->set_header_main();
							$this->set_navigation(array('<i class="icon-home"></i>หน้าแรก' => base_url(),
														'สมาชิก' => base_url() . 'member',
														'ประวัติการสั่งซื้อ' => base_url() . 'member/payment/'));
							
							$display_length = 10;						
							$page_index = $this->input->get('p');
							if (empty($page_index) || $page_index < 0)
								$page_index = 0;
							else
								$page_index--;							
							
							$data_menu['user'] = $this->member_info();
							$data_menu['active_menu'] = 'payment';
									
							$this->load->model('local/data/mod_package_order');
							$data_content['data'] = $this->mod_package_order->get_payment_history($this->session->userdata('MEM_ID'), $display_length, ($display_length * $page_index));
							
							$data_content['page_count'] = ceil($this->mod_package_order->get_payment_order_count($this->session->userdata('MEM_ID')) / $display_length);				
							$data_content['page_index'] = $page_index;
									
							$this->template->write('left_sidebar', $this->load->view('widget/member/frm_user_info', $data_menu, true));
							$this->template->write('content_1', $this->load->view('member/frm_member_payment', $data_content, true));									
							$this->template->render();
						} else
							redirect(base_url() . 'member');
					}
					break;
			}			
		}
		function favorite(){
			$opr = $this->uri->segment(3);
			switch ($opr){
				case 'ajax_remove_favorite_cafe':{	
					$result = false;
					$message = null;
					$redirect_url = null;
									
					$this->load->library('define/operation_message');
					if ($this->check_member_session()){
						$this->load->model('local/data/cafe/mod_cafe');
						$cafe_id = $this->mod_cafe->get_cafe_id_from_title($this->input->post('c'));
						
						$this->load->model('local/data/mod_member');
						if ($this->mod_member->remove_favorite_cafe($cafe_id, $this->session->userdata('MEM_ID'))){
							$result = true;
							$message = $this->operation_message->msg_remove_success;
						} else
							$message = $this->operation_message->msg_remove_fail;
					} else {
						$result = -1;
						$message = $this->operation_message->msg_relogin;
						$redirect_url = base_url() . 'member';
					}
					
					print_r(json_encode(array('result' => $result,
												'message' => $message,
												'redirect_url' => $redirect_url)));
						
				}
				break;
				default:{
					if ($this->check_member_session()){
						$this->set_header_main();
						$this->set_navigation(array('<i class="icon-home"></i> หน้าแรก' => base_url(),
													'สมาชิก' => base_url() . 'member',
													'ร้านกาแฟโปรด' => base_url() . 'member/favorite'));
						
						$display_length = 10;
						$page_index = $this->input->get('p');
						if (empty($page_index) || $page_index < 0)
							$page_index = 0;
						else
							$page_index--;
						
						$data_menu['user'] = $this->member_info();
						$data_menu['active_menu'] = 'favorite';
						
						$this->load->model('local/data/mod_member');
						$data_content['data'] = $this->mod_member->get_favorite_cafe($this->session->userdata('MEM_ID'), $display_length, $page_index, 
																						array('CAF_TITLE','cafe.CAF_ID','CAF_IMG_L_FILENAME','CAF_LAST_ACTIVE',
																								'CAF_ULDATE_INFO','CAF_ULDATE_GAL','CAF_ULDATE_PDT','CAF_ULDATE_CON'));
						
						$this->load->library('custom_datetime');
						$data_content['curr_date'] = $this->custom_datetime->current_datetime();						
						$data_content['page_count'] = ceil($this->mod_member->get_favorite_cafe_count($this->session->userdata('MEM_ID')) / $display_length);
						$data_content['page_index'] = $page_index;						
						
						$this->template->write('left_sidebar', $this->load->view('widget/member/frm_user_info', $data_menu, true));
						$this->template->write('content_1', $this->load->view('member/frm_member_favorite', $data_content, true));
						$this->template->render();
					} else
						redirect(base_url() . 'member');
				}
				break;
			}		
		}
		
		// Authenrize		
		function fb_login(){
			$this->load->library('facebook_api');
			
			$user = $this->facebook_api->get_user();	
			if ($user) {				
				$this->load->model('local/data/mod_member');
								
				// register member or login member
				if (!$this->mod_member->check_if_member_registered_on_facebook($user['id'])){
					
					// register member
					$this->load->library('custom_datetime');					
					$this->mod_member->MEM_TITLE = $user['first_name'] . ' ' . $user['last_name'];					
					$this->mod_member->MEM_REGIS_DATE = $this->custom_datetime->current_date();
					
					$gender = 0;
					if ($user['gender'] == 'male')
						$gender = 1;
					else
						$gender = 2;
										
					$this->mod_member->MEM_EMAIL = $user['email'];
					$this->mod_member->MEM_GENDER = $gender;
					$this->mod_member->MEM_ADDRESS = $user['location']['name'];
					$this->mod_member->MEM_FB_UID = $user['id'];
					$this->mod_member->add();
				
					// set session
					$data = array('MEM_ID' => $this->mod_member->db()->last_query(),
									'MEM_NAME' => $user['first_name'] . ' ' . $user['last_name'],
									'FB_USER_ID' => $user['id'],
									'IS_LOGGED_IN' => true);
					$this->session->set_userdata($data);
				} else {					
					
					$this->load->model('local/data/mod_member');
					$member = $this->mod_member->get_member_by_facebook_uid($user['id']);
					
					// set session
					if (!is_null($member)){
						$data = array('MEM_ID' => $member['MEM_ID'],
										'MEM_NAME' => $member['MEM_TITLE'],
										'FB_USER_ID' => $user['id'],										
										'IS_LOGGED_IN' => true);
						$this->session->set_userdata($data);
					}					
				}			
			}
			
			redirect(base_url() . 'member');
		}		
		function logout(){
			parent::logout();
			redirect(base_url() . 'member');
		}	
	}
		
?>